package swu.edu.cn.javaee.controller;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import swu.edu.cn.javaee.entity.Article;
import swu.edu.cn.javaee.entity.Topic;
import swu.edu.cn.javaee.service.TopicService;
import java.util.List;
import static swu.edu.cn.javaee.component.Signal.*;

@Controller
//解决跨域问题：允许所有源域名访问
@CrossOrigin(origins = "*")

public class TopicController {
    @Resource
    private TopicService topicService;

    //新建帖子
    @RequestMapping("/saveTopic")
    @ResponseBody
    public String saveTopic(String title,String content,String author){
        String result=topicService.saveTopic(title,content,author);
        if (result!=null){
            return result;
        }
        else {
            return FAILED_CODE;
        }
    }

    //根据标题查找帖子
    @RequestMapping("/queryTopicByTitle")
    @ResponseBody
    public List<Topic> queryTopicByTitle(String title){
        List<Topic> topicList=topicService.queryTopicByTitle(title);
        if (topicList!=null){
            return topicList;
        }
        else {
            return null;
        }
    }

    //删除帖子
    @RequestMapping("/deleteTopic")
    @ResponseBody
    public String deleteTopic(String tid){
        String result=topicService.deleteTopic(tid);
        return result;
    }
}
